<?php

namespace app\admin\service;

use app\admin\traits\TableChangeTrait;
use app\common\model\AccessLog;
use app\common\model\Consulting;
use app\common\model\LawCase;
use app\common\model\LogCaptcha;
use app\common\model\Member;
use app\common\model\Trade;
use ShidianUtil\Utils\TimeUtil;

/**
 * 全链路数据
 * Class LinkTableService
 * @package app\admin\service
 */
class LinkTableService
{
    use TableChangeTrait;

    public function index($startTime, $endTime)
    {
        $timeList = TimeUtil::timeList(TimeUtil::DAY, $startTime, $endTime, true);

        $days = [];
        $list = [];
        foreach ($timeList as $item) {
            $s = $item['s_time'];
            $e = $item['e_time'];

            $days[] = ['name' => date('j', $s) . '日'];

            $list[] = $this->getData($s, $e);
        }

        return [
            'day_count' => count($days),
            'days' => $days,
            'list' => $this->columnAndRowSwitch($list),
        ];
    }

    /**
     * 获取数据
     * @param $startTime
     * @param $endTime
     * @return array
     */
    private function getData($startTime, $endTime): array
    {
        $pv = '';
        $uv = '';
        $captcha_count = '';
        $login_register_rate = '';
        $login_member_count = '';
        $new_member_count = '';
        $uv_register_rate = '';
        $free_consulting_count = '';
        $law_case_count = '';
        $pay_count = '';
        $pay_rate = '';
        $pay_success_count = '';
        $register_pay_rate = '';
        $pay_success_sum = '';
        $refund_success_count = '';
        $lawyer_money_sum = '';
        $refund_success_sum = '';
        $lawyer_money_success_sum = '';
        $uv_value = '';
        $unpay_count = '';
        $per_money = '';
        $refund_rate = '';

        if ($startTime < time()) {

            $pv = AccessLog::timeRange($startTime, $endTime, 'created', 'created')->count();
            // 获取时间区间的全部数据

            $uv = AccessLog::timeRange($startTime, $endTime, 'created', 'created')->group('ip')->count();

            // 验证码数
            $captcha_count = LogCaptcha::timeRange($startTime, $endTime, 'created', 'created')
                ->where('scene', CAPTCHA_DENGLU)
                ->group('phone')
                ->count();

            // 新增用户（注册数）
            $new_member_count = Member::timeRange($startTime, $endTime, 'created', 'created')->count();

            // 有多少人登录
            $login_member_count = AccessLog::timeRange($startTime, $endTime, 'created', 'created')
                ->where('memberId', '<>', '0')
                ->group('memberId')
                ->count();

            // 免费咨询数
            $free_consulting_count = Consulting::timeRange($startTime, $endTime, 'created', 'created')
                ->where('consultingType', '0')
                ->where('status', '1')
                ->count();

            // 案源数
            $law_case_count = LawCase::timeRange($startTime, $endTime, 'created', 'created')
                ->count();

            // 发起支付数
            $pay_count = Trade::timeRange($startTime, $endTime, 'created', 'created')
                ->consultingTrade()
                ->count();

            // 支付成功数
            $pay_success_count = Trade::timeRange($startTime, $endTime, 'created', 'created')
                ->consultingTrade()
                ->where('payStatus', Trade::PAID)
                ->count();

            // 支付成功金额
            $pay_success_sum = Trade::timeRange($startTime, $endTime, 'created', 'created')
                ->consultingTrade()
                ->where('payStatus', Trade::PAID)
                ->sum('payMoney');

            // 退款成功订单数
            $refund_success_count = Trade::timeRange($startTime, $endTime, 'created', 'created')
                ->consultingTrade()
                ->where('payStatus', Trade::PAID)
                ->whereIn('refundStatus', [Trade::REFUNDED, Trade::REFUNDED])
                ->where('refundCheckStatus', Trade::AGREED)
                ->count();

            // 退款成功金额
            $refund_success_sum = Trade::timeRange($startTime, $endTime, 'created', 'created')
                ->consultingTrade()
                ->where('payStatus', Trade::PAID)
                ->whereIn('refundStatus', [Trade::REFUNDED, Trade::REFUNDED])
                ->where('refundCheckStatus', Trade::AGREED)
                ->sum('payMoney');

            // 应返佣金
            $lawyer_money_sum = Trade::timeRange($startTime, $endTime, 'created', 'created')
                ->consultingTrade()
                ->where('payStatus', Trade::PAID)
                ->whereIn('refundStatus', [Trade::NOTREFUND, Trade::REFUNDING, Trade::REFUND_FAILED])
                ->sum('lawyerMoney');

            // 成功返佣
            $lawyer_money_success_sum = Trade::timeRange($startTime, $endTime, 'created', 'created')
                ->consultingTrade()
                ->where('payStatus', Trade::PAID)
                ->where('settleStatus', Trade::SETTLED)
                ->sum('settleLawyerMoney');

            // 发起退款订单数
            $refund_count = Trade::timeRange($startTime, $endTime, 'created', 'created')
                ->consultingTrade()
                ->where('payStatus', Trade::PAID)
                ->where('refundStatus', '<>', Trade::NOTREFUND)
                ->count();

            // 退款率
            $refund_rate = show_percentage($refund_count, $pay_success_count, 1);

            // 登录注册率
            $login_register_rate = show_percentage($new_member_count, $captcha_count, 1);

            // 访问注册率
            $uv_register_rate = show_percentage($new_member_count, $uv, 1);

            // 发起支付率
            $pay_rate = show_percentage($pay_success_count, $pay_count, 1);

            // 注册支付率
            $register_pay_rate = show_percentage($pay_success_count, $new_member_count, 1);

            // 单UV价值
            $uv_value = show_float($pay_success_sum, $uv);

            // 注册未支付数
            $unpay_count = $new_member_count - $pay_success_count;

            // 客单价=支付总额/支付单数
            $per_money = show_float($pay_success_sum, $pay_success_sum);

        }

        return [
            [
                'title' => 'PV',
                'value' => $pv
            ],
            [
                'title' => 'UV',
                'value' => $uv
            ],
            [
                'title' => '验证码数',
                'value' => $captcha_count
            ],
            [
                'title' => '登录注册率',
                'value' => $login_register_rate
            ],
            [
                'title' => '登录总数',
                'value' => $login_member_count
            ],
            [
                'title' => '注册总数',
                'value' => $new_member_count
            ],
            [
                'title' => '访问注册率',
                'value' => $uv_register_rate
            ],
            [
                'title' => '免费咨询数',
                'value' => $free_consulting_count
            ],
            [
                'title' => '案源数',
                'value' => $law_case_count
            ],
            [
                'title' => '发起支付数',
                'value' => $pay_count
            ],
            [
                'title' => '发起支付率',
                'value' => $pay_rate
            ],
            [
                'title' => '支付成功数',
                'value' => $pay_success_count
            ],
            [
                'title' => '注册支付率',
                'value' => $register_pay_rate
            ],
            [
                'title' => '支付成功金额',
                'value' => $pay_success_sum
            ],
            [
                'title' => '退款成功订单数',
                'value' => $refund_success_count
            ],
            [
                'title' => '退款成功金额',
                'value' => $refund_success_sum
            ],
            [
                'title' => '应返佣金',
                'value' => $lawyer_money_sum
            ],
            [
                'title' => '成功返佣',
                'value' => $lawyer_money_success_sum
            ],
            [
                'title' => '单UV价值',
                'value' => $uv_value
            ],
            [
                'title' => '注册未支付数',
                'value' => $unpay_count
            ],
            [
                'title' => '客单价',
                'value' => $per_money
            ],
            [
                'title' => '退款率',
                'value' => $refund_rate
            ],
        ];
    }
}
